#!/bin/bash
#
# fxcmis      This shell script takes care of starting and stopping
#               fxcmis (on 8006)
#
#
# chkconfig: 2345 99 20

# Source function library.
. /etc/init.d/functions

getpid() {
  echo `ps -ef|grep -w $PORT|grep -v grep|awk '{printf $2}'`
}

FXCMIS_DIR=/opt/cmis-server
PIDFILE=/var/run/fxcmis.pid
prog=fxcmis
PORT=8006
PID=`getpid`

if [ -z $PID ] && [ -e ${PIDFILE} ]; then
  rm ${PIDFILE}
fi

if [ -z $PID ] && [ -e /var/lock/subsys/$prog ]; then
  rm -f /var/lock/subsys/$prog
fi

start() {

	    PID=`getpid`

	    if [ $PID ]; then
                echo Foxit CMIS Server already running.
        else
                cd $FXCMIS_DIR
               java -Xmx1500M -Xms512M -XX:PermSize=64M -XX:MaxPermSize=512M -cp /opt/jetty-run/jetty-runner.jar org.eclipse.jetty.runner.Runner  --port 8006 --log /var/log/cmis.log chemistry-opencmis-server-jcr-1.0.0-SNAPSHOT.war >/var/log/cmis-console.log 2>&1 &
#                java -Xmx1500M -Xms512M -XX:PermSize=64M -XX:MaxPermSize=512M -jar jetty-runner.jar --port 8006 --log /var/log/cmis.log chemistry-opencmis-server-jcr-1.0.0-SNAPSHOT.war >/dev/null 2>&1 &
		echo $! > ${PIDFILE}
                RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
	    	  action $"Starting $prog: " /bin/true
	    	  touch /var/lock/subsys/$prog
		else
	    	  action $"Starting $prog: " /bin/false
		fi
                return $RETVAL
        fi

}

stop() {
	PID=`getpid`
	if [ -z $PID ]; then
	  echo "Foxit CMIS Server already stopped"
	else
          kill -9 $PID
          action $"Stopping $prog: " /bin/true
	fi
        rm -f /var/lock/subsys/$prog ${PIDFILE}
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p ${PIDFILE} $prog
        ;;
    restart)
        stop
	sleep 5
        start
        ;;
    *)
        echo "Usage:  {start|stop|status|restart}"
        exit 1
        ;;
esac
exit $?
